package com.whfc.wxmp.api.base.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.util.RandomUtil;
import com.whfc.wxmp.api.base.service.WxFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class WxFileServiceImpl implements WxFileService {

    /**
     * 小程序图片地址
     */
    private String machImgPath = "wxmp/img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    /**
     * 小程序中图片上传
     *
     * @throws IOException
     */
    @Override
    public String uploadImg(MultipartFile file) throws IOException {
        String fileName = RandomUtil.getRandomFileName() + ".png";
        String ossKey = filePathConfig.getFilePath(machImgPath, fileName);
        String upload = fileHandler.upload(ossKey, file.getInputStream());
        return upload;
    }

}
